其他
一个可以画星星图层的R包,了解下
ggstar
从接触ggplot2
作图开始,我就疑惑,为什么ggplot2
没有画五角星,六角星,
七角星的点……甚至连很多正多边形的点都没有。matplotlib
倒是有支持几种但样式不是很喜欢。这几天,我为了更深入了解ggplot2
的原理,就写了这个包。从一开始的只能画五角星,图片长宽比变化时会变形《学下图层,收下这颗小星星》,到目前已经实现可以像geom_point
那样的功能了。所以在画点图的时候就可以考虑用ggstar
来代替。特别是当需要很多可识别的形状的时候。
安装
目前ggstar
还没有发行版,因为我周五才刚提交到CRAN
。想要体验的可以通过下面方式安装。
if(!requireNamespace("remotes"))
install.packages("remotes")
remotes::install_github("xiangpin/ggstar")
使用
目前ggstar
只开放geom_star
以及对应的scale
函数出来,使用方法很简单,基本与ggplot2
中的geom_point
类似。只不过geom_star
中控制图形样式的参数是starshape
。对应的manual
方法是scale_starshape_manual
。此外还有starstroker
控制线条粗细,fill
控制颜色填充,color
控制线条颜色,size
控制形状大小。geom_star`目前支持的图形样式如下:
library(ggplot2)
library(ggstar)
p <- show_starshapes()
p
上面太抽象?来个实例大概演示下。
实例
以微生物组学研究中常见的pcoa
分析为例,用简单散点图可视化下
library(ggplot2)
library(ggstar)
library(vegan)
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.5-4
# load data
# feature row X sample col
dat <- read.table("./datasets/demo_data.txt", header=T, sep="\t", row.names=1)
sampledt <- read.table("./datasets/sampledata.txt", header=T, sep="\t", row.names=1)
dat <- data.frame(t(dat))
# standardization using decostand of vegan
dat <- decostand(dat, method="log")
# calculate the distance between the samples
sampledist <- vegdist(dat, "bray")
# pcoa analysis using pcoa of ape
pcoares <- ape::pcoa(sampledist)
# build the coordinate
pcoaplotcord <- data.frame(pcoares$vectors[, 1:3])
colnames(pcoaplotcord) <- c("PC1", "PC2", "PC3")
pcoaplotcord <- merge(pcoaplotcord, sampledt, by=0)
# the label of x and y
ev <- pcoares$values
vp <- ev$Relative_eig * 100
xlab_text <- paste("PC1 (", round(vp[1],2), "%)")
ylab_text <- paste("PC2 (", round(vp[2],2), "%)")
# plotting
p1 <- ggplot()+
geom_vline(xintercept = 0,linetype='dashed',size=0.5)+
geom_hline(yintercept = 0,linetype='dashed',size=0.5)+
# 这边开始用到geom_star, starshape, fill 分别控制形状与颜色填充。
geom_star(data=pcoaplotcord,
aes(x=PC1,
y=PC2,
starshape=condition1,
fill=condition2),
size=6)+
theme_bw()+
# scale_starshape_manual 是对形状类型的定义
scale_starshape_manual(values=c(1, 2, 9))+
xlab(xlab_text) +
ylab(ylab_text) +
labs(title="PCoA - PC1 vs PC2 (Bray-Curtis)") +
theme(legend.title = element_text(size=7),
legend.text= element_text(size=6),
legend.key.height=unit(0.1,"mm"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(face="bold",
lineheight=25,
hjust=0.5,
size=10))
p1
以后更新方向
可能会考虑加上阴影效果。
logo
最后用下我老板的hexSticker
包,做个logo图
library(hexSticker)
library(ggplot2)
library(ggstar)
data <- data.frame(x = rep(c(1:4), 3), y = rep(1:3, each = 4,
len = 12), group = letters[c(1:12)])
p <- ggplot(data=iris, aes(x=Sepal.Length,y=Sepal.Width,
starshape=Species, fill=Species)) + geom_star(show.legend=FALSE, size=1.8) +
scale_fill_manual(values=c("#66C2A5", "#FC8D62", "#8DA0CB"))
p <- p + theme_void() + theme(axis.line.x.bottom = element_line(color="white", size=0.22),
axis.line.y.left = element_line(color="white", size=0.22)) + theme_transparent()
sticker(p, package="ggstar", p_size=14, s_x=1, s_y=.75, s_width=1.3, s_height=0.68,
h_fill="black", h_color="#B3B3B3", p_color="#FFDF00",
filename="ggstarlog.png")